package com.intmall.train.${module}.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.IterUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.intmall.train.common.context.LoginMemberContext;
import com.intmall.train.common.domain.resp.CommonPageResp;
import com.intmall.train.common.util.SnowUtil;
import com.intmall.train.${module}.domain.po.${Domain};
import com.intmall.train.${module}.domain.po.${Domain}Example;
import com.intmall.train.${module}.domain.req.${Domain}QueryReq;
import com.intmall.train.${module}.domain.req.${Domain}SaveReq;
import com.intmall.train.${module}.domain.resp.${Domain}QueryResp;
import com.intmall.train.${module}.mapper.${Domain}Mapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class ${Domain}Service {
    @Resource
    private ${Domain}Mapper ${domain}Mapper;

    public void save(${Domain}SaveReq req) {
        DateTime now = DateTime.now();
        if (ObjectUtil.isNull(req.getId())) {
            ${Domain} ${domain} = BeanUtil.copyProperties(req, ${Domain}.class);
            ${domain}.setMemberId(LoginMemberContext.getId());
            ${domain}.setId(SnowUtil.getSnowflakeNextId());
            ${domain}.setCreateTime(now);
            ${domain}.setUpdateTime(now);
            ${domain}Mapper.insert(${domain});
        } else {
            ${Domain}Example ${domain}Example = new ${Domain}Example();
            ${Domain}Example.Criteria criteria = ${domain}Example.createCriteria();
            criteria.andMemberIdEqualTo(LoginMemberContext.getId());
            criteria.andIdEqualTo(req.getId());
            List<${Domain}> ${domain}s = ${domain}Mapper.selectByExample(${domain}Example);
            if (IterUtil.isNotEmpty(${domain}s)) {
                ${Domain} ${domain} = ${domain}s.get(0);
                ${domain}.setName(req.getName());
                ${domain}.setIdCard(req.getIdCard());
                ${domain}.setType(req.getType());
                ${domain}.setUpdateTime(now);
                ${domain}Mapper.updateByPrimaryKeySelective(${domain});
            }
        }
    }

    public void delete(Long id) {
        ${Domain}Example ${domain}Example = new ${Domain}Example();
        ${Domain}Example.Criteria criteria = ${domain}Example.createCriteria();
        criteria.andMemberIdEqualTo(LoginMemberContext.getId());
        criteria.andIdEqualTo(id);
        ${domain}Mapper.deleteByExample(${domain}Example);
    }

    public CommonPageResp<${Domain}QueryResp> queryList(${Domain}QueryReq req) {
        ${Domain}Example ${domain}Example = new ${Domain}Example();
        ${domain}Example.setOrderByClause("id desc");
        ${Domain}Example.Criteria criteria = ${domain}Example.createCriteria();
        if (ObjectUtil.isNotNull(req.getMemberId())) {
            criteria.andMemberIdEqualTo(req.getMemberId());
        }

        PageHelper.startPage(req.getPage(), req.getLimit());
        List<${Domain}> ${domain}List = ${domain}Mapper.selectByExample(${domain}Example);
        PageInfo<${Domain}> pageInfo = new PageInfo<>(${domain}List);

        List<${Domain}QueryResp> list = BeanUtil.copyToList(${domain}List, ${Domain}QueryResp.class);
        return CommonPageResp.SUCCESS("", list, pageInfo);
    }
}
